package com.company.indicationshots.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.company.indicationshots.R;
import com.company.indicationshots.services.CommServices;
import com.target.communication.service.CommSer;
import com.target.communication.service.ICommData;
import com.target.communication.util.StringUtil;

import java.io.IOException;


public class TestActivity extends AppCompatActivity {
    private final String TAG = "TestActivity";

    private CommSer commBinder;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onBindingDied(ComponentName name) {
            ServiceConnection.super.onBindingDied(name);
        }

        //Activity与Service断开连接时回调该方法
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected");
        }

        //Activity与Service连接成功时回调该方法
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected");
            commBinder = (CommSer) service;

            commBinder.setiDataSerial(new ICommData() {
                @Override
                public void dataCallBack(byte[] var1) {
                    //接收数据
                    String s = StringUtil.bytesToHex(var1);
                    Log.i(TAG, "data from " + s);
                }
            });
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        Log.d(TAG, "onCreate");
//        TestComm.connSerial();

    }

    private void unbindService() {
        if (commBinder == null) {
            Log.i(TAG, "unbindService,binder is null");
            return;
        }
        unbindService(conn);
        commBinder = null;
    }

    /*
     *   绑定服务
     * */
    private void bindService() {
        Intent intent = new Intent(this, CommServices.class);
        intent.putExtra("param", "param1");
        Log.i(TAG, "bindService");
        bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService();
        Log.i(TAG, "onDestroy");
    }

    public void conns(View view) throws IOException {
        bindService();
    }

    public void openSerial(View view) throws IOException {
        commBinder.connSerial();
    }

    public String[] getSerial(View view) throws IOException {
        String[] serialDevicesPath = commBinder.getSerialDevicesPath();
        Log.i(TAG, "serialDevicesPath:" + serialDevicesPath.toString());
        return serialDevicesPath;
    }

    public void targetShow(View view) throws IOException {
        String s = "01050037FF003DF4";
        commBinder.send(s);
    }

}